package com.newyear.LiveCommerceManagement.servlet;

import entity.LiveCommerce;
import service.LiveCommerceService;
import service.impl.LiveCommerceServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author 吴阳基
 * @datetime 2023/11/13 10:24
 */
@WebServlet(urlPatterns = "/add")
public class AddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");

        LiveCommerce liveCommerce = new LiveCommerce();
        liveCommerce.setStreamer(req.getParameter("streamer"));
        liveCommerce.setLivePlatform(req.getParameter("livePlatform"));
        liveCommerce.setProduct(req.getParameter("product"));
        liveCommerce.setLiveTime(req.getParameter("liveTime"));
        liveCommerce.setPrice(Double.parseDouble(req.getParameter("price")));
        liveCommerce.setStatus(Long.parseLong(req.getParameter("status")));

        LiveCommerceService liveCommerceDao = new LiveCommerceServiceImpl();
        int add = liveCommerceDao.add(liveCommerce);

        PrintWriter out = resp.getWriter();
        if (add > 0){
            out.println("<script>alert('添加成功！');location.href = 'list'</script>");
        }else {
            out.println("<script>alert('添加失败！');history.back()</script>");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
